<?php

namespace app\api\controller;

use app\model\Config;
use think\Controller;
use think\facade\Cache;
use think\Request;

class Index extends Controller
{
    public function getIndexData(Request $request)
    {
        $data = [];
        $config = $this->getConfig();
        $data['config'] = $config['data'];
        $banner = $this->getBanner();
        $data['banner'] = $banner['data'];
        $course = $this->getCourse($request);
        $data['course'] = $course['data'];
        $category = Db('category')->order('sort asc')->limit(2)->select();
        foreach ($category as $key => $val) {
            $category[$key]['article'] = Db('article')->where('category_id', $val['category_id'])->order('sort asc')->limit(2)->select();
        }
        $data['category'] = $category;
        return ['code' => 0, 'data' => $data];
    }

    public function getConfig()
    {
        $config = [];
        $config['app_name'] = Cache::remember('app_name', Config::getConfigValue('app_name'));
        $config['app_description'] = Cache::remember('app_description', Config::getConfigValue('app_description'));
        $config['app_host'] = Cache::remember('app_host', Config::getConfigValue('app_host'));
        $config['phone'] = Cache::remember('phone', Config::getConfigValue('phone'));
        return ['data' => $config, 'code' => 0];
    }

    public function getBanner()
    {
        $banner = Db('banner')->order('sort asc')->select();
        return ['data' => $banner, 'code' => 0];
    }

    public function getCategory()
    {
        $category = Db('category')->order('sort asc')->select();
        return ['data' => $category, 'code' => 0];
    }

    public function getCourse(Request $request)
    {
        $course = Db('course')
            ->alias('c')
            ->join('category ct','c.category_id = ct.category_id','LEFT')
            ->where('c.status', 1)
            ->where('c.start_time', 'gt', date('Y-m-d'))
            ->field('c.course_id,c.name,c.image,c.price,c.start_time,c.end_time,c.address,c.description,ct.name as category_name')
            ->order('c.category_id asc,c.course_id desc')
            ->select();
        $token = $request->param('token');
        if ($token && $user = Db('user')->where('token', $token)->field('token', true)->find()) {
            $course_ids = Db('order')->where('user_id', $user['user_id'])->where('status', 1)->column('course_id');
            foreach ($course as $key => $val) {
                if (in_array($val['course_id'], $course_ids)) {
                    $course[$key]['is_buy'] = 1;
                } else {
                    $course[$key]['is_buy'] = 0;
                }
            }
        }
        return ['data' => $course, 'code' => 0];
    }

    public function getHotWord()
    {
        $course = Db('search')
            ->order('num desc')
            ->limit(10)
            ->select();
        return ['data' => $course, 'code' => 0];
    }

    public function getAbout()
    {
        $config['about'] = Cache::remember('about', Config::getConfigValue('about'));
        $config['phone'] = Cache::remember('phone', Config::getConfigValue('phone'));
        return ['data' => $config, 'code' => 0];
    }
}